package com.frame.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.frame.Page;
import com.frame.Result;
import com.frame.domain.Permission;
import com.frame.domain.Role;
import com.frame.domain.RolePermission;
import com.frame.service.PermissionService;
import com.frame.service.RolePermissionService;
import com.frame.service.RoleService;
import com.frame.support.Tree;
import com.mini.core.utils.StrKit;

/**
 * @author sxjun1904
 * @since 2016-8-8
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController{

    private static final Logger log = LoggerFactory.
            getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;
    
    @Autowired
    private PermissionService permissionService;
    
    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 角色页面
     * @param model
     * @param request
     * @param response
     * @param treeId
     * @return
     */
    @RequiresPermissions("role:list")
    @RequestMapping(value = "list")
    public String list(Model model, HttpServletRequest request, HttpServletResponse response,String treeId) {
    	Page<Role> page = roleService.paginateResult(new Page<Role>(request, response),treeId);
    	addPage(model,page);
        return "role/roleList";
    }
    
    /**
     * 角色表单
     * @param model
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping(value = "form")
    public String form(Model model, HttpServletRequest request, HttpServletResponse response,String id) {
    	if(StrKit.notBlank(id)){
    		Role m = roleService.findById(id);
    		model.addAttribute("role", m);
    	}
        return "role/roleForm";
    }
    
    /**
     * 角色保存
     * @param role
     * @return
     */
    @RequestMapping(value = "save")
    @ResponseBody
    public Result save(Role role) {
    	if(StrKit.notBlank(role.getId()))
    		roleService.update(role);
    	else{
    		role.setCreatetime(new Date());
        	roleService.save(role);
    	}
        return success();
    }
    
    /**
     * 角色删除
     * @param id
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public Result delete(String id) {
    	roleService.delete(id);
        return success();
    }
    
    /**
     * 保存角色权限
     * @param ids
     * @param roleid
     * @return
     */
    @RequestMapping(value = "saveroleperm")
    @ResponseBody
    public Result saveroleperm(String ids,String roleid){
    	roleService.deleteRolePermissonByRoleid(roleid);
    	String[] _ids = ids.split(",");
    	for(String id : _ids){
    		RolePermission roleperm = new RolePermission();
    		roleperm.setRoleid(roleid);
    		roleperm.setPermissionid(id);
    		roleperm.setCreatetime(new Date());
    		roleService.saveRolePermisson(roleperm);
    	}
    	return success();
    }
    
   /**
    * 菜单角色树
    * @param roleid
    * @return
    */
   @RequestMapping(value = "permtree")
   @ResponseBody
   public List<Tree> permtree(String roleid) {
	    List<Permission> perms = permissionService.findAll();
	    List<RolePermission> roleperms = null;
	    if(StrKit.notBlank(roleid))
	    	roleperms = rolePermissionService.findByRoleId(roleid);
	    List<Tree> trees = new ArrayList<Tree>();
		for(Permission perm : perms){
			Tree tree = new Tree();
			tree.setId(perm.getId());
			tree.setName(perm.getPermname());
			if(StrKit.notBlank(perm.getParentid()))
				tree.setpId(perm.getParentid());
			else
				tree.setOpen(true);
			if(roleperms!=null)
				for(RolePermission roleperm:roleperms){
					if(roleperm.getPermissionid().equals(perm.getId())){
						tree.setChecked(true);
						break;
					}
				}
			trees.add(tree);
		}
		return trees;
   }
    
}
